﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace imageDome
{
    public partial class Form1 : Form
    {
        //布尔型变量，是否正在绘图
        //private bool isDrawing = false;
        //绘图时记录鼠标的位置
        //private Point startPoint, oldPoint;
        ////当前编辑的文件名
        //private string editFileName;
        //进行操作的位图
        private Image theImage;
        ////绘制位图的Graphics实例
        //private Graphics ig;
        //绘图使用的色彩
        private Color foreColor = Color.Red;
        private Color backColor = Color.White;
        public string[,] points = new string[10, 10];
        private int number = 0, numberH = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pushNum(@"F:\Documents and Settings\guangya\桌面\q.txt");
        }

        public void ReadFile(string fileUrl)
        {
            int i = 0;
            StreamReader r;
            try
            {
                r = File.OpenText(fileUrl);
            }
            catch
            {
                MessageBox.Show("文件打开失败！");
                return;
            }
            char[] delimit = new char[] { ' ' };
            //points[1,1] = r.ReadToEnd().ToString().Split(delimit);
            while (r.Peek() != -1)
            {
                string[] pointc = new string[10];
                pointc = r.ReadLine().Split(delimit);
                if (i < 10)
                {

                    for (int j = 0; j < 10; j++)
                    {
                        points[i, j] = pointc[j];
                        //MessageBox.Show(pointc[j]);
                    }
                    //MessageBox.Show("ser");
                    ++i;
                }
                else
                {
                    i = 0;
                }
            }
            r.Close();
        }

        public void WriteFile()
        {
            StreamWriter w;

            try
            {
                w = File.CreateText(@"F:\Documents and Settings\guangya\桌面\11111.txt");
            }
            catch
            {
                return;
            }

            for (int i = 0; i < 10; i++)
            {
                string str = "";
                for (int j = 0; j < 10; j++)
                {
                    str += points[i, j] + " ";
                }
                w.WriteLine(str);
            }
            w.Close();
        }

        public void pushNum(string fileUrl)
        {
            ReadFile(fileUrl);
            SolidBrush sBrush = new SolidBrush(foreColor);
            Graphics g = this.pictureBox1.CreateGraphics();
            Font theFont = this.Font;
            //MessageBox.Show(Convert.ToDouble((this.pictureBox1.Height / 10) * (1 + 1)).ToString());
            for (int y = 0; y < 10; y++)
            {
                for (int x = 0; x < 10; x++)
                {
                    if (points[x, y] != null)
                    {
                        g.DrawString(points[x, y].ToString(), theFont, sBrush, ((600 / 10) * (y)) + 30, ((450 / 10) * (x)) + 22);
                    }
                    //
                }
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            if (theImage != null)
            {
                g.Clear(Color.White);
                g.DrawImage(theImage, this.pictureBox1.ClientRectangle);
            }
        }

        private void Form1_ClientSizeChanged(object sender, EventArgs e)
        {
            this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //如果选择文字输入，则打开strInput窗体
                strInput inputBox = new strInput();
                inputBox.StartPosition = FormStartPosition.CenterParent;
                if (inputBox.ShowDialog() == DialogResult.OK)
                {
                    SolidBrush sBrush = new SolidBrush(foreColor);
                    Graphics g = this.pictureBox1.CreateGraphics();
                    Font theFont = this.Font;
                    g.DrawString(inputBox.textBox1.Text, theFont, sBrush, e.Y, e.X);
                    //ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
                    number = ((e.X * 10) / this.pictureBox1.Width);
                    numberH = ((e.Y * 10) / this.pictureBox1.Height);
                    points[numberH, number] = inputBox.textBox1.Text;
                    WriteFile();
                    this.pictureBox1.Refresh();
                    pushNum(@"F:\Documents and Settings\guangya\桌面\11111.txt");
                    //this.pictureBox1.Image=bitmap;
                    //System.Threading.Thread.Sleep(100);
                }
                //MessageBox.Show(points[numberH,number]);
            }
        }
    }
}
